package com.operate.api.operate.banner.service.impl;

import com.operate.api.app.dramas.domain.vo.DramaTrailersVO;
import com.operate.api.app.info.domain.vo.AppBannerSearchVO;
import com.operate.api.app.info.domain.vo.AppBannerVO;
import com.operate.api.base.Constants;
import com.operate.api.base.Type;
import com.operate.api.operate.banner.dao.BannerDao;
import com.operate.api.operate.banner.domain.po.BannerPO;
import com.operate.api.operate.banner.domain.vo.BannerAddVO;
import com.operate.api.operate.banner.domain.vo.BannerSearchVO;
import com.operate.api.operate.banner.domain.vo.BannerUpdateVO;
import com.operate.api.operate.banner.domain.vo.BannerVO;
import com.operate.api.operate.banner.service.IBannerService;
import com.operate.api.operate.dramas.dao.VideoDao;
import com.operate.api.operate.dramas.domain.vo.VideoSearchVO;
import com.operate.api.operate.dramas.domain.vo.VideoVO;
import com.operate.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerDao dao;


    @Autowired
    private VideoDao videoDao;


    @Override
    public List<BannerVO> getList(BannerSearchVO searchVO) {
        return dao.getList(searchVO);
    }

    @Override
    public void add(BannerAddVO vo) {
        BannerPO po = new BannerPO();
        BeanUtils.copyProperties(vo, po);
        po.preInsert();
        int i = dao.insertSelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void update(BannerUpdateVO vo) {
        BannerPO po = dao.selectByPrimaryKey(vo.getId());
        if(po==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        BeanUtils.copyProperties(vo, po);
        po.preUpdate();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void topping(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        BannerPO po = dao.selectByPrimaryKey(id);
        if(po==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        po.setBannerSort(dao.getMaxSort());
        po.preUpdate();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        int i = dao.deleteByPrimaryKey(id);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public List<VideoVO> getVideoList(VideoSearchVO searchVO) {
        return videoDao.getBannerList(searchVO);
    }

    @Override
    public List<AppBannerVO> getInfoBannerList(AppBannerSearchVO searchVO) {
        return dao.getInfoBannerList(searchVO);
    }
}